// pages/rank-bar/rank-bar.js
import {RankType} from "../../core/enum"
import {getSystemSize} from "../../utils/system"
import {Rank} from "../../model/rank"
import {User} from "../../model/user"
import {formatSecond} from "../../utils/util"

Page({

    data: {
	    type: RankType.ANSWER,
	    rankListHeight: Number,
		studyList:[],
		answerList:[]
    },
	async onSwitchType(event) {
		// wx.showLoading({
		// 	mask: true
		// })
		// const user = new User()
		if (event.detail.type === RankType.STUDY) {
			// let studyRanks
			// studyRanks = await Rank.getStudyRank()
			// studyRanks.map(item => {
			// 	item.formatDuraion = formatSecond(item.duration)
			// })
			// const studyRankSelf = studyRanks.find(item => {
			// 	return item.user.id === user.user.id
			// })
			// if (studyRanks.length > 50) {
			// 	studyRanks = studyRanks.slice(0, 50)
			// }
			// wx.hideLoading()
			this.setData({
				// studyList: studyRanks,
				// studyRankSelf,
				type: event.detail.type
			})
		} else {
			// let answerRanks
			// answerRanks = await Rank.getAnswerRank()
			// const answerRankSelf = answerRanks.find(item => {
			// 	return item.user.id === user.user.id
			// })
			// if (answerRanks.length > 50) {
			// 	answerRanks = answerRanks.slice(0, 50)
			// }
			// wx.hideLoading()
			this.setData({
				// answerList: answerRanks,
				// answerRankSelf,
				type: event.detail.type
			})
		}
	},

	onGoToRules: function() {
        wx.navigateTo({
	        url: `/pages/rankRule/rankRule`
        })
	},
	onLoad: async function() {
		wx.showLoading({
			mask: true
		})
    	this.setDynamicSegmentHeight()
		const user = new User()
		// let studyRanks
		// studyRanks = await Rank.getStudyRank()
		// studyRanks.map(item => {
		// 	item.formatDuraion = formatSecond(item.duration)
		// })
		// const studyRankSelf = studyRanks.find(item => {
		// 	return item.user.id === user.user.id
		// })
		// if (studyRanks.length > 50) {
		// 	studyRanks = studyRanks.slice(0, 50)
		// }

		let answerRanks
		answerRanks = await Rank.getAnswerRank()
		const answerRankSelf = answerRanks.find(item => {
			return item.user.id === user.user.id
		})
		if (answerRanks.length > 50) {
			answerRanks = answerRanks.slice(0, 50)
		}
		this.setData({
			// studyList: studyRanks,
			// studyRankSelf,
			answerList: answerRanks,
			answerRankSelf
		})
		wx.hideLoading()
	},
	async onShow() {

	},
	async setDynamicSegmentHeight() {
		const res = await getSystemSize()
		const screenHeight = res.screenHeight
		const h = screenHeight
		this.setData({
			rankListHeight: h
		})
		console.log(wx.getSystemInfoSync().screenHeight)


	},
})
